<?php

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 后台用户控制器
 */
class SignController extends AdminController {

	public function index(){

        $get = $_GET;
        if($get['keywords']!=''){
            $where['bhy_comment.content|u.username'] = array('like','%'.$get['keywords'].'%');
        }
        $time = timeCondition($get['start_time'],$get['end_time']);
        if($time!=0){
            $where['bhy_comment.create_time'] = $time;
        }
        $where['bhy_comment.status'] = array('gt',-1);
        $where['bhy_comment.pid']    = array('eq',0);
        $where['bhy_comment.type']   = array('eq',2);
        //分页
        $listsCount = M('comment')->join('bhy_usermember as u on u.id=bhy_comment.uid')->where($where)->count();
        $Page       = new \Think\Page($listsCount,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $lists = M('comment')->join('bhy_usermember as u on u.id=bhy_comment.uid')->where($where)->order('bhy_comment.id desc')->field('bhy_comment.*,u.username,u.user_type')->limit($Page->firstRow.','.$Page->listRows)->select();
        $tmes = C('WEB_FAKUAN_TIME');
        foreach ($lists as $ke=>$ve){
            $lists[$ke]['publish_time'] = $ve['publish_time']>0?date('Y-m-d H:i',$ve['publish_time']):'未发布';
            if($ve['publish_time']>0){
                if(($ve['publish_time']-$ve['create_time'])>$tmes*3600){
                    $publish = signOutTime($ve['id']);
                }else{
                    $publish = '正常';
                }
            }else{
                    if(time()-$ve['create_time']>$tmes*3600){
                        $publish = signOutTime($ve['id']);
                    }else{
                        $publish = '正常';
                    }
            }

            $lists[$ke]['fakuan']  = $publish;
        }

        $this->assign('list',$lists);
        $this->assign('_page',$show);
        $this->display();
		
	}
    public function _before_add(){
        $_POST['create_time'] = time();
    }
    public function _before_edit(){
        $_POST['update_time'] = time();
    }

    //罚款
    public function fakuan(){
        if(IS_POST){
            $post = $_POST;
            if($post['id']>0 && floatval($post['value'])>0){
                $usermember = M('usermember');
                $flowater = M('flowater');
                $usermember->startTrans(); $flowater->startTrans(); //开启事物

                $uid = modelField($post['id'],'comment','uid');

                $vo = M('usermember')->find($uid);
                $data['id'] =  $vo['id'];
                $data['balance'] = $vo['balance']-floatval($post['value']);
                $res = $usermember->save($data);

                //添加流水表
                $datas['type']   = 2;
                $datas['flow_type']   = 2;
                $datas['return_id'] = $post['id'];
                $datas['uid']    = $uid;
                $datas['price']  = floatval($post['value']);
                $datas['create_time']  = time();
                $datas['remark']  = '未按时发布信息';
                $rest = $flowater->add($datas);

                if($res && $rest){
                    $usermember->commit(); $flowater->commit(); //开启事物
                    $arr = array('code'=>200,'msg'=>'罚款成功');
                }else{
                    $usermember->rollback(); $flowater->rollback(); //开启事物
                    $arr = array('code'=>201,'msg'=>'罚款失败');
                }
                $this->ajaxReturn($arr);
            }else{
                $arr = array('code'=>201,'msg'=>'参数错误');
                $this->ajaxReturn($arr);
            }
        }
    }

    //查看
    public function see(){
        $get = $_GET;
        $vo = M('comment')->find($get['id']);
        $vo['imgarr'] = imgarr($vo['imgarr']);
        $this->assign('vo',$vo);
        $this->display();
    }




}
